<template>
  <div class="recommend" ref="recommend">
    <scroll class="recommend-content" :data="discLists" ref="scroll">
      <div>
        <div class="slider-wrapper" v-if="recommends.length">
          <slider>
            <div v-for="item in recommends">
              <a :href="item.linkUrl">
                <img :src="item.picUrl"  @load="loadImg" class="needsclick">
              </a>
            </div>
          </slider>
        </div>
        <div class="recommend-list">
          <h1 class="list-title">热门歌单推荐</h1>
          <ul>
            <li v-for="item in discLists" class="list-item" @click="selectItem(item)">
              <div class="icon">
                <img v-lazy="item.imgurl" height="60" width="60">
              </div>
              <div class="text">
                <h2 class="name" v-html="item.creator.name"></h2>
                <p class="desc" v-html="item.dissname"></p>
              </div>
            </li>
          </ul>
        </div>
      </div>
      <div class="loading-content" v-show="!discLists.length">
        <loading></loading>
      </div>
    </scroll>
    <router-view></router-view>
  </div>
</template>
<script type="text/ecmascript-6">
  import Scroll from 'base/scroll/scroll'
  import Slider from 'base/slider/slider'
  import {getRecommend, getDiscList} from 'api/recommend'
  import {ERR_OK} from 'api/config'
  import Loading from 'base/load/load'
  import {playListMixin} from 'common/js/mixin.js'
  import {mapMutations} from 'vuex'

  export default({
    mixins: [playListMixin],
    data() {
      return {
        recommends: [],
        discLists: [],
        checkLoaded: false
      }
    },
    created() {
      this._getRecommend()
      setTimeout(() => {
        this._getDiscList()
      }, 1000)
    },
    methods: {
      _getRecommend() {
        getRecommend().then((res) => {
          if (res.code === ERR_OK) {
            this.recommends = res.data.slider
          }
        })
      },
      _getDiscList() {
        getDiscList().then((res) => {
          console.log(res)
          if (res.code === ERR_OK) {
            this.discLists = res.data.list
          }
        })
      },
      selectItem(item) {
        this.$router.push({
          path: `/recommend/${item.dissid}`
        })
        this.setDisc(item)
      },
      loadImg() {
        if (!this.checkLoaded) {
          this.$refs.scroll.refresh()
          this.checkLoaded = true
        }
      },
      handlePlayList(playList) {
        const bottom = playList.length > 0 ? '60px' : ''
        this.$refs.recommend.style.bottom = bottom
        this.$refs.scroll.refresh()
      },
      ...mapMutations({
        setDisc: 'SET_DISC'
      })
    },
    components: {
      Slider,
      Scroll,
      Loading
    }
  })
</script>

<style scoped lang="stylus" rel="stylesheet/stylus">
  @import "~common/stylus/variable"
  .recommend
    position: fixed
    width: 100%
    top: 88px
    bottom: 0
   .recommend-content
     height: 100%
     overflow: hidden
  .list-title
    text-align center
    color: $color-theme
    margin: 20px 0

  .list-item
    display: flex
    overflow: hidden
    align-content center
    clear: both;
    .icon
      padding: 10px 20px
      flex: 0 0 60px
      float: left
    .text
      display: flex
      flex-direction column
      justify-content center
      line-height: 20px
      font-size $font-size-medium
      text-align justify-content
      .name
        margin-bottom 10px
        font-size: 14px
      .desc
        color: $color-text-d
        font-size: 12px
        line-height: 15px
        overflow hidden

  .loading-content
    position: absolute
    width:100%
    top:50%
    transform : translateY(-50%)
</style>
